#include "Menu_db.h"


Menu_db::Menu_db(const string &n)
{
	db = new Database(n);
	submenu = 0;
}


Menu_db::~Menu_db(void)
{
	if(db != 0) {
		delete db;
	}
}

void Menu_db::start()
{
	int chosen = 0;

	do
	{
		switch(chosen)
		{
		case 1:
			{
				table_list();
				break;
			}
		case 2:
			{
				table_load(true);
				break;
			}
		case 3:
			{
				table_load(false);
				break;
			}
		case 4:
			{
				table_drop();
				break;
			}
		case 5:
			{
				db_drop();
				break;
			}
		case 6:
			{
				db_truncate();
				break;
			}
		}

		cout << endl << "### Database: " << db->get_name() << " ###" << endl;
		cout << "1. List tables" << endl;
		cout << "2. Add table" << endl;
		cout << "3. Load table" << endl;
		cout << "4. Drop table" << endl;
		cout << "5. Drop db" << endl;
		cout << "6. Truncate db" << endl;
		cout << "7. Back" << endl;
		chosen = Validator::get<int>("Please choose a number: ");
		system("cls");
	} while(chosen != 7);
}


void Menu_db::db_drop() const
{
	db->drop(true);
}

void Menu_db::db_truncate() const
{
	db->truncate();
}

void Menu_db::table_list() const
{
	vector<string> list = db->list_tables();
	cout << "### Table list ###" << endl;
	for(vector<string>::iterator it = list.begin(); it != list.end(); ++it)
		cout << *it << endl;
	cout << endl;
}

void Menu_db::table_load(const bool& newtable)
{
	if(db->empty() && !newtable)
	{
		cout << "No table in db" << endl;
	}
	else
	{
		Table* t = db->load_table(newtable);
		if(t != NULL)
		{
			submenu = new Menu_table(t);
			submenu->start();
			delete submenu;
		}
	}
}

void Menu_db::table_drop() const
{
	db->drop(false);
}